package com.example.demo.Dao;

import com.example.demo.Pojo.Department;
import com.example.demo.Pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

//员工Dao
@Repository
public class EmployeeDao {
    //    模拟数据库中的数据
    private static Map<Integer, Employee> exceployees = null;
    //    员工有所属部门
    @Autowired
    private DepartmentDao departmentDao;

    static {
        exceployees = new HashMap<>();
        exceployees.put(1001, new Employee(1001, "aa", "A1234566@qq.com", 1, new Department(101, "部门1")));
        exceployees.put(1002, new Employee(1002, "aa", "B1234566@qq.com", 0, new Department(102, "部门2")));
        exceployees.put(1003, new Employee(1003, "aa", "C1234566@qq.com", 1, new Department(103, "部门3")));
        exceployees.put(1004, new Employee(1004, "aa", "D1234566@qq.com", 0, new Department(104, "部门4")));
        exceployees.put(1005, new Employee(1005, "aa", "E1234566@qq.com", 0, new Department(105, "部门5")));
    }

    private static Integer initId = 1006;

    //    增加一个员工
    public void save(Employee employee) {
        if (employee.getId() == null) {
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
        exceployees.put(employee.getId(), employee);
    }

    //    查询员工信息
    public Collection<Employee> getAll() {
        return exceployees.values();
    }

    //    通过id查询员工
    public Employee getEmployeeByid(Integer id ) {
        return exceployees.get(id);
    }
}
